package com.zyf.workflow.test.work;

import com.zyf.workflow.business.fwupgrade.FwUpgradeBean;
import com.zyf.workflow.business.fwupgrade.PackageWorker;
import com.zyf.workflow.node.Node;
import com.zyf.workflow.node.WorkNode;
import com.zyf.workflow.node.bean.NodeType;
import com.zyf.workflow.work.AbstractWork;
import com.zyf.workflow.work.Order;
import com.zyf.workflow.work.bean.WorkTypeEnum;
import com.zyf.workflow.worker.Worker;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class FwUpgradeWork<T> extends AbstractWork<List<FwUpgradeBean>> {

    public FwUpgradeWork(List<FwUpgradeBean> data,String workName) {
        super(data,workName);
    }


    // todo 考虑引入Order注解，通过构造函数自动生成List<Nodes>
    @Order(1)
    private Node<String> getPackageNode(List<FwUpgradeBean> fwUpgradeBeans) {
        PackageWorker packageWorker = new PackageWorker();
        Worker<List<FwUpgradeBean>, String, String> worker = packageWorker::packageIso;

        return new WorkNode.Builder<List<FwUpgradeBean>, String>()
                .withWorker(worker)
                .withNodeId(1)
                .withData(fwUpgradeBeans)
                .withName("package ISO Node")
                .setCallBack((data,param) -> {
                    System.out.println("fwUpgrade node finished");
                    System.out.println(".....");
                })
                .withType(NodeType.WORK)
                .create();
    }

    private Node<String> getMountNode(List<FwUpgradeBean> fwUpgradeBeans) {
        PackageWorker packageWorker = new PackageWorker();
        Worker<List<FwUpgradeBean>, String, Map<String, String>> worker = packageWorker::mountIso;
        return new WorkNode.Builder<List<FwUpgradeBean>, String>()
                .withWorker(worker)
                .withNodeId(2)
                .withData(fwUpgradeBeans)
                .withName("mount ISO node")
                .setCallBack((data,param) -> {
                    System.out.println("mount iso node finished");
                    System.out.println(".....");
                })
                .withType(NodeType.WORK)
                .create();

    }


    //    @SuppressWarnings("unchecked")
    private Node<Map<String, String>> getConfigNode(List<FwUpgradeBean> fwUpgradeBeans) {
        PackageWorker packageWorker = new PackageWorker();
        Worker<List<FwUpgradeBean>, Map<String, String>, Void> worker = packageWorker::config;
        return new WorkNode.Builder<List<FwUpgradeBean>, Map<String, String>>()
                .withWorker(worker)
                .withNodeId(3)
                .withData(fwUpgradeBeans)
                .withName("config param node")
                .setCallBack((data,param) -> {
                    System.out.println("config param node finished");
                    System.out.println(".....");
                })
                .withType(NodeType.WORK)
                .create();
    }


    private Node<String> getRestartNode(List<FwUpgradeBean> fwUpgradeBeans) {
        PackageWorker packageWorker = new PackageWorker();
        Worker<List<FwUpgradeBean>, String, Void> worker = packageWorker::restart;
        return new WorkNode.Builder<List<FwUpgradeBean>, String>()
                .withWorker(worker)
                .withNodeId(4)
                .withData(fwUpgradeBeans)
                .withName("restart server node")
                .setCallBack((data,param) -> {
                    System.out.println("restart server node finished");
                    System.out.println(".....");
                })
                .withType(NodeType.WORK)
                .create();
    }


    private Node<String> getProcessNode(List<FwUpgradeBean> fwUpgradeBeans) {
        PackageWorker packageWorker = new PackageWorker();
        Worker<List<FwUpgradeBean>, String, Void> worker = packageWorker::getProcess;
        return new WorkNode.Builder<List<FwUpgradeBean>, String>()
                .withWorker(worker)
                .withNodeId(5)
                .withData(fwUpgradeBeans)
                .withName("get upgrade process node")
                .setCallBack((data,param) -> {
                    System.out.println("get upgrade process node finished");
                    System.out.println(".....");
                })
                .withType(NodeType.WORK)
                .create();
    }


    @Override
    public List<Node<?>> initNodes(List<FwUpgradeBean> data) {

        List<FwUpgradeBean> fwUpgradeBeanList = (List<FwUpgradeBean>) data;

        List<Node<?>> nodes = new ArrayList<>();
        Node<String> packageNode = getPackageNode(fwUpgradeBeanList);
        Node<String> mountNode = getMountNode(fwUpgradeBeanList);
        Node<Map<String, String>> configNode = getConfigNode(fwUpgradeBeanList);
        Node<String> restartNode = getRestartNode(fwUpgradeBeanList);
        Node<String> processNode = getProcessNode(fwUpgradeBeanList);
        nodes.add(packageNode);
        nodes.add(mountNode);
        nodes.add(configNode);
        nodes.add(restartNode);
        nodes.add(processNode);

        return nodes;
    }

    @Override
    public WorkTypeEnum getWorkType() {
        return WorkTypeEnum.WORK;
    }
}
